const { ccclass, property, requireComponent } = cc._decorator;

@ccclass
export default class FollowTargetCamera extends cc.Component {

    @property()
    speed: number = 0
    @property()
    sensitivity: number = 0

    @property({ type: cc.Node })
    target: cc.Node = null

    public autoUpdate: boolean = true

    private _speed: number = 0
    private _dir: cc.Vec2 = cc.v2(0, 0)
    updataPosition(dt: number) {
        if (this.target) {
            let worldPos = this.target.getParent().convertToWorldSpaceAR(this.target.getPosition())
            let localPos = this.node.getParent().convertToNodeSpaceAR(worldPos)
            this._dir.x = localPos.x
            this._dir.y = localPos.y
            this._speed = this._dir.mag() / dt
            this.node.setPosition(localPos)
        }
    }
    update(dt: number) {
        if (this.autoUpdate) {
            this.updataPosition(dt)
        }
    }
}